import Vue from "vue";
import Router from "vue-router";
import store from "../store/index";

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/index"
    },
    // 首页
    {
      path: "/index",
      component: () => import("@/views/index/index"),
      meta: {
        title: "一品数码会"
        // keepAlive: true
      }
    },
    // 物品详情
    {
      path: "/goodsDetail",
      meta: {
        title: "物品详情",
        keepAlive: false
      },
      component: () => import("@/views/goodsDetail/index")
    },
    // 暗拍首页 darkPat
    {
      path: "/darkPat",
      component: () => import("@/views/darkPat/index")
    },
    // 保修查询 warrantyQuery
    {
      path: "/warrantyQuery",
      meta: {
        title: "保修查询"
      },
      component: () => import("@/views/warrantyQuery/index")
    },
    // 保证金 ensureMoney
    {
      path: "/ensureMoney",
      meta: {
        title: "保证金"
      },
      component: () => import("@/views/ensureMoney/index")
    },
    // 保证金记录 ensureMoneyRecord
    {
      path: "/ensureMoneyRecord",
      meta: {
        title: "保证金记录"
      },
      component: () => import("@/views/ensureMoneyRecord/index")
    },
    // 公告 announcement
    {
      path: "/announcement",
      meta: {
        title: "公告"
      },
      component: () => import("@/views/announcement/index")
    },
    // 公告详情 announcementDetail
    {
      path: "/announcementDetail",
      meta: {
        title: "公告详情"
      },
      component: () => import("@/views/announcementDetail/index")
    },
    // 平台规则 terraceRules
    {
      path: "/terraceRules",
      meta: {
        title: '平台规则'
      },
      component: () => import("@/views/terraceRules/index")
    },
    // 等级说明 levelThat
    {
      path: "/levelThat",
      meta: {
        title: '等级说明'
      },
      component: () => import("@/views/levelThat/index")
    },

    // 消息
    {
      path: "/information",
      meta: {
        title: "消息"
      },
      component: () => import("@/views/information/index")
    },
    // 个人
    {
      path: "/user",
      meta: {
        title: "个人"
      },
      component: () => import("@/views/user/index")
    },
    // 个人信息 userInformation
    {
      path: "/userInformation",
      meta: {
        title: "个人信息"
      },
      component: () => import("@/views/userInformation/index")
    },
    // 我的订单 myOrder
    {
      path: "/myOrder",
      meta: {
        title: "我的订单"
      },
      component: () => import("@/views/myOrder/index")
    },
    // 我的包裹 myParcel
    {
      path: "/myParcel",
      meta: {
        title: "我的包裹"
      },
      component: () => import("@/views/myParcel/index")
    },
    // 我的订单 -> 订单详情  orderDetail
    {
      path: "/orderDetail",
      meta: {
        title: "订单详情"
      },
      component: () => import("@/views/orderDetail/index")
    },
    // 我的包裹 -> 订单详情 orderDetails
    {
      path: "/orderDetails",
      meta: {
        title: "订单详情"
      },
      component: () => import("@/views/orderDetails/index")
    },
    // 核对订单 checkOrder
    {
      path: "/checkOrder",
      meta: {
        title: "核对订单"
      },
      component: () => import("@/views/checkOrder/index")
    },
    // 查看物流 showLogistics
    {
      path: "/showLogistics",
      meta: {
        title: "查看物流"
      },
      component: () => import("@/views/showLogistics/index")
    },
    // 支付成功 paySuccess
    {
      path: "/paySuccess",
      meta: {
        title: "支付成功"
      },
      component: () => import("@/views/paySuccess/index")
    },
    // 所有宝贝 allTreasure
    {
      path: "/allTreasure",
      meta: {
        title: "所有宝贝"
      },
      component: () => import("@/views/allTreasure/index")
    },
    // 买家收货地址 buyersAddress
    {
      path: "/buyersAddress",
      meta: {
        title: "买家收货地址"
      },
      component: () => import("@/views/buyersAddress/index")
    },
    // 卖家退货地址 sellerReturnAddress
    {
      path: "/sellerReturnAddress",
      meta: {
        title: "卖家退货地址"
      },
      component: () => import("@/views/sellerReturnAddress/index")
    },

    // {
    //   path: "/my",
    //   component: () => import("@/views/my/index"),
    //   redirect: "/my/center",
    //   children: [
    //     {
    //       path: "center",
    //       meta: {
    //         title: "个人中心"
    //       },
    //       component: () => import("@/views/my/children/center")
    //     },
    //     {
    //       path: "set",
    //       meta: {
    //         title: "设置"
    //       },
    //       component: () => import("@/views/my/children/set")
    //     }
    //   ]
    // },
    {
      path: "/login",
      component: () => import("@/views/login/index"),
      meta: {
        title: "登陆"
      }
    },
    {
      path: "*",
      component: () => import("@/components/NotFound")
    }
  ]
});

router.beforeEach((to, from, next) => {
  let { title, needLogin } = to.meta;
  let { isLogin } = store.state;
  document.title = title;

  if (needLogin && !isLogin) {
    next({
      path: "/login"
    });
  } else {
    next();
  }
});

router.afterEach(() => {
  document.body.scrollTop = 0;
  document.documentElement.scrollTop = 0;
});

export default router;
